package com.tenoz.display
{
	import de.popforge.ui.Slider;
	import de.popforge.ui.SliderWithButtons;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class Menu extends Sprite
	{
		public var sliderV: SliderWithButtons;
		private var xml:XML;
		private var items:Array = [];
		private var visibleArea:Sprite;
		private var containor:Sprite;
		
		public function Menu()
		{
			this.visibleArea = new Sprite();
			this.visibleArea.graphics.beginFill(0);
			this.visibleArea.graphics.drawRect(0,0,10,10);
			this.visibleArea.graphics.endFill();
			this.addChild(this.visibleArea);
			
			this.containor = new Sprite();
			this.containor.mask = this.visibleArea;
		}
		
	/**
	 * Method setXML
	 * 
	 * @param _xml:XML - the <menu> node found in the config XML document.
	 **/
		public function setXML(_xml:XML):void
		{
			xml = _xml;
			var leng:int = xml.items.item.length();
			var item:MenuItem;
			var wid:int = 0;
			for(var i:int=0; i<leng; i++)
			{
				item = new MenuItem();
				item.setTitle(xml.items.item[i].name);
				item.type = xml.items.item[i].type;
				item.id = xml.items.item[i].id;
				
				item.addEventListener(MouseEvent.MOUSE_OVER, onOver);
				item.addEventListener(MouseEvent.MOUSE_OUT, onOut);
				
				if(wid < item.width)
				{
					wid = item.width;
				}
				
				items[i] = item;
			}
			
			for(i=0; i<leng; i++)
			{
				items[i].width = wid;
				items[i].y = 27*i;
				this.containor.addChild(items[i]);
			}
			
		/*Create VisibleArea Mask*/
			this.visibleArea.width = wid;
			this.visibleArea.height = 400;
			
			this.graphics.lineStyle(1,0x808080,1);
			this.graphics.moveTo(-1,-1);
			this.graphics.lineTo(this.visibleArea.width,-1);
			this.graphics.lineTo(this.visibleArea.width,this.visibleArea.height);
			this.graphics.lineTo(-1,this.visibleArea.height);
			this.graphics.lineTo(-1,-1);
			
			this.addChild(this.containor);
		
		/*Create Vertical Slider*/
//			this.sliderV = new SliderWithButtons( this.visibleArea.height, 14 );
//			this.sliderV.x = this.visibleArea.width;
//			this.sliderV.y = 0;
//			this.sliderV.slider.onChanged = this.onVSliderChanged;
//			this.sliderV.slider.setKnobSize( this.visibleArea.height / this.containor.height );
//			this.addChild( this.sliderV );
			
//			this.sliderV.topCap.addEventListener(MouseEvent.CLICK, topClick);
//			this.sliderV.botCap.addEventListener(MouseEvent.CLICK, botClick);
		}
		
//		private function topClick(e:MouseEvent):void
//		{
//			if(this.containor.y < -27)
//			{
//				this.containor.y += 27; //27px is the height of each menu item.
//			}
//			else
//			{
//				this.containor.y = 0;
//			}
//			this.setVSlider();
//		}
//		
//		private function botClick(e:MouseEvent):void
//		{
//			if(this.containor.y > -(this.containor.height-this.visibleArea.height)+27)
//			{
//				this.containor.y -= 27; //27px is the height of each menu item.
//			}
//			else
//			{
//				this.containor.y = -(this.containor.height-this.visibleArea.height);
//			}
//			this.setVSlider();
//		}
		
		private function onOver(e:MouseEvent):void
		{
			var item:MenuItem = e.currentTarget as MenuItem;
			item.setColor(0xE6E6E6);
		}
		
		private function onOut(e:MouseEvent):void
		{
			var item:MenuItem = e.currentTarget as MenuItem;
			item.setColor(0xFFFFFF);
		}
		
//		private function onVSliderChanged( slider: Slider ): void
//		{
//			var value: Number = slider.getValue();
//			this.containor.y = -( this.containor.height - this.visibleArea.height ) * value;
//		}
//		
//		private function setVSlider(): void
//		{
//			var value: Number = this.containor.y / -( this.containor.height - this.visibleArea.height );
//			this.sliderV.slider.setValue(value);
//		}
		
//		public function setValue( value:Number ): void
//		{
//			this.sliderV.slider.setValue(value);
//		}
		
		public function getItems():Array
		{
			return this.items;
		}
		
		public function onResize(stageWidth:Number, stageHeight:Number):void
		{
			this.visibleArea.height = this.containor.height;
			
			this.graphics.clear();
			this.graphics.lineStyle(1,0x808080,1);
			this.graphics.moveTo(-1,-1);
			this.graphics.lineTo(this.visibleArea.width,-1);
			this.graphics.lineTo(this.visibleArea.width,this.visibleArea.height);
			this.graphics.lineTo(-1,this.visibleArea.height);
			this.graphics.lineTo(-1,-1);
			
//			this.sliderV.setBarSize( this.visibleArea.height, this.visibleArea.height / this.containor.height );
		}
	}
}